package com.util;

import java.io.Serializable;
import java.util.*;

/**
 * @author 王乾
 * @comments 随机工具类
 * @date 2024-05-10 22:39:25
 */
public class UtilRandom implements Serializable {

    private static final char[] CHARS = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
            'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};


    private static final char[] CHARS_NUMBER = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};


    /**
     * @author 王乾
     * @comments Java.uuid主键
     * @time 2024-05-10 22:39:25
     * @returnType String
     * @modification
     */
    public static String uuid(){
        String s = UUID.randomUUID().toString();
        return s.substring(0,8)+s.substring(9,13)+s.substring(14,18)+s.substring(19,23)+s.substring(24);
    }


    /**
     * @author 王乾
     * @comments 随机生成n位由字母或数字组成的字符串
     * @date 2024-05-10 22:39:25
     * @param length
     * @return java.lang.String
     * @modification
     */
    public static String randomGenerate(int length) {
        List<String> list = new ArrayList<>(CHARS.length);
        for (int i = 0; i < CHARS.length; i++) {
            list.add(String.valueOf(CHARS[i]));
        }
        Collections.shuffle(list);
        int count = 0;
        StringBuffer sb = new StringBuffer();
        Random random = new Random(System.nanoTime());
        while (count < length) {
            int i = random.nextInt(list.size());
            sb.append(list.get(i));
            count++;
        }
        return sb.toString();
    }

    /**
     * @author 王乾
     * @comments  获取随机长度数字的字符串
     * @param length
     * @date 2024-05-10 22:39:25
     * @return java.lang.String
     * @modification
     */
    public static String randomNumber(int length) {
        List<String> list = new ArrayList<>(CHARS_NUMBER.length);
        for (int i = 0; i < CHARS_NUMBER.length; i++) {
            list.add(String.valueOf(CHARS_NUMBER[i]));
        }
        Collections.shuffle(list);
        int count = 0;
        StringBuffer sb = new StringBuffer();
        Random random = new Random(System.nanoTime());
        while (count < length) {
            int i = random.nextInt(list.size());
            sb.append(list.get(i));
            count++;
        }
        return sb.toString();
    }

    /**
     * @author 王乾
     * @comments 生成指定范围随机数
     * @date 2024-05-10 22:39:25
     * @modification
     */
    public static int randInt(int min,int max) {
        return new Random().nextInt(max-min+1)+min;
    }

}
